<?php
/**
 * Copyright (c) [2019] [吴跃忠]
 * [selibra] is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 * http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

namespace Selibra\Http\Lib;


use Selibra\Di\Container\Exception\MetadataNotFoundException;
use Selibra\Di\DI;
use Selibra\Http\Exception\ResponseException;
use Selibra\Http\Session;
use Selibra\Config\Config;
use Selibra\Tools\Console;

abstract class HttpController
{


    /**
     * @var Request
     */
    private Request $request;


    /**
     * @var Response
     */
    private Response $response;


    /**
     * @var Session
     */
    private Session $session;


    /**
     * 开始
     */
    public function __begin()
    {
        $this->__init();
    }


    /**
     * @param Request $request
     */
    public function __setRequest(Request $request): void
    {
        $this->request = $request;
    }


    /**
     * @param Response $response
     */
    public function __setResponse(Response $response): void
    {
        $this->response = $response;
    }


    /**
     * 控制器初始化
     * @return void
     */
    protected function __init()
    {

    }


    /**
     * 获取Session实例
     * @return Session
     */
    public function session()
    {
        return $this->session;
    }


    /**
     * @param Session $session
     * @return $this
     */
    public function __setSession(Session $session)
    {
        $this->session = $session;
        return $this;
    }

    /**
     * @return Request
     */
    protected function getRequest(): Request
    {
        return $this->request;
    }


    /**
     * @return Response
     */
    protected function getResponse(): Response
    {
        return $this->response;
    }


    /**
     * @param null $key
     * @return array|null
     */
    protected function server($key = null)
    {
        return $this->getRequest()->server($key);
    }


    /**
     * @param $key
     * @param null $value
     * @param array $ucwords
     * @return mixed
     */
    protected function header($key, $value = null, $ucwords = [])
    {
        return $this->getResponse()->header($key, $value, $ucwords);
    }


    /**
     * 跳转地址
     * @param $url
     * @throws ResponseException
     */
    protected function redirect($url)
    {
        $this->getResponse()->setStatus(302)->redirect($url);
        return $this->getResponse();
    }


    /**
     * 成功返回
     * @param $data
     * @return null
     */
    protected function success($message, $data = null, $code = 0)
    {
        $this->getResponse()
            ->setMessage($message)
            ->setStatus(200)
            ->setContent($data)
            ->setCode($code);
        return $this->getResponse();
//        throw new ResponseException($this->getResponse());
    }


    /**
     * 失败返回
     * @param $data
     * @return null
     * @throws
     */
    protected function error($message, $data, $code)
    {
        $this->getResponse()
            ->setMessage($message)
            ->setStatus(200)
            ->setContent($data)
            ->setCode($code);
        return $this->getResponse();
    }


    /**
     * @param string $id
     * @return object
     */
    protected function container(string $id)
    {
        return DI::getObjectContext($id);
    }
}